package com.teatime.client.image;

import com.teatime.client.Constants;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;

public class MaskImageView extends ImageView {

	private int      mMaskWidth;
	private int      mMaskHeight;
	private Rect     mMaskRect;
	private Paint 	 mShadePaint;
	private Paint    mOutlinePaint;
	private Paint 	 mTextPaint;
	private int      mFitWidth;
	private String   mMaskText;
	
	
	public enum MaskMode { None, Left, Top, Right, Bottom, Center, Full };
	private MaskMode mMaskMode;
	
	public MaskImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}
	
	public MaskImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}
	
	protected void init() {
		setScaleType(ScaleType.CENTER_INSIDE);
		mFitWidth = -1;
		mMaskRect = new Rect();
		mMaskMode = MaskMode.None;
		mMaskWidth = 0;
		mMaskHeight = 0;
		mMaskText = null;
		mShadePaint = new Paint();
		mShadePaint.setARGB(170, 123, 125, 127);
		mTextPaint = new Paint();
		mTextPaint.setColor(Color.WHITE);
		mOutlinePaint = new Paint();
		mOutlinePaint.setStrokeWidth(3F);
        mOutlinePaint.setStyle(Paint.Style.STROKE);
        mOutlinePaint.setColor(Color.BLACK);
	}
	
	public void setMask(MaskMode maskMode, int width, int height) {
		mMaskMode = maskMode;
		mMaskWidth = width;
		mMaskHeight = height;
	}
	
	public void removeMask() {
		mMaskMode = MaskMode.None;
	}
	
	public void setFitWidth(int fitWidth) {
		mFitWidth = fitWidth;
	}
	
	public void setMaskText(String maskText) {
		mMaskText = maskText;
	}
	
	public void setMaskText(int maskTextId) {
		mMaskText = getContext().getResources().getString(maskTextId);
	}
	
	public void setMaskTextSize(float textSize) {
		mTextPaint.setTextSize(textSize);
	}
	
	public void setMaskTextBold(boolean isBold) {
		mTextPaint.setFakeBoldText(isBold);
	}
	
	@Override
	protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
		int width, height;
		if (mFitWidth > 0) {
			width = mFitWidth;
			height = (int)((float)width / Constants.ImageWHRation);
			setMeasuredDimension(width, height);
		}
		else if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY 
		    && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY ) {
			width = MeasureSpec.getSize(widthMeasureSpec);
			height = (int)((float)width / Constants.ImageWHRation);
			setMeasuredDimension(width, height);
		}
		else {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}
		
	}
	
	@Override  
    protected void onDraw(Canvas canvas) {  
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        /*if (mDrawable != null) {
        	TLog.v("drawable: " + mDrawable.getBounds().width() +
            		"X" + mDrawable.getBounds().height() +
            		", win: " + getWidth() + "X" + getHeight());
        	//mDrawable.setBounds(0, 0, getWidth(), getHeight());
        	//mDrawable.draw(canvas);
        	
        }*/
        
        int maskWidth, maskHeight;
        maskWidth = Math.min(mMaskWidth, getWidth());
        maskWidth = Math.max(1, maskWidth);
        maskHeight = Math.min(mMaskHeight, getHeight());
        maskHeight = Math.max(1, maskHeight);
        /*Rect bounds = mBounds;
        if (bounds == null) {
        	bounds = new Rect(getLeft(), getTop(), getRight(), getBottom());
        }*/
        
        switch(mMaskMode) {
        case Full:
        	mMaskRect.set(0, 0, getWidth() - 1 , getHeight() - 1);
        	break;
        case Left:
        	mMaskRect.set(0, 0, maskWidth - 1, getHeight() - 1);
        	break;
        case Top:
        	mMaskRect.set(0, 0, getWidth() - 1, maskHeight - 1);
        	break;
        case Right:
        	mMaskRect.set(getWidth() - maskWidth, 0, 
        			getWidth() - 1, getHeight() - 1);
        	break;
        case Bottom:
        	mMaskRect.set(0, getHeight() - maskHeight, 
        			getWidth() - 1, getHeight() - 1);
        	break;
        case Center:
        	mMaskRect.set(getWidth() - maskWidth / 2, 
        			getHeight() - maskHeight / 2, 
        			getWidth() - maskWidth / 2 + maskWidth - 1, 
        			getHeight() - maskHeight / 2 + maskHeight - 1);
        	break;
		default:
			break;
        }
        
        if (mMaskMode != MaskMode.None) {
        	
        	canvas.drawRect(mMaskRect, mShadePaint);
        }
        
        if (mMaskText != null && mMaskText != "") {
        	float x, y;
        	FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        	int textHeight = (int)(fontMetrics.bottom - fontMetrics.top) + 1,
        		textWidth  = (int)mTextPaint.measureText(mMaskText);
        	x = (getWidth() - textWidth) / 2;
        	if (mMaskMode != MaskMode.None) {
        		// drawText is align with bottom
        		y = mMaskRect.top + 
        			(mMaskRect.height() - textHeight) / 2 + 
        			textHeight;
        	}
        	else{
        		y = (getHeight() - textHeight) / 2;
        	}
        	canvas.drawText(mMaskText, x, y, mTextPaint);
        }

        canvas.drawRect(0, 0, getWidth() - 1 , getHeight() - 1, mOutlinePaint);
    }

}
